package com.peach.inquiry.imsdk.rtc;

import android.os.Bundle;
import android.view.View;

import com.peach.inquiry.IM;
import com.tencent.rtmp.ui.TXCloudVideoView;
import com.tencent.trtc.TRTCCloud;
import com.tencent.trtc.TRTCCloudDef;
import com.tencent.trtc.TRTCCloudListener;

import static com.tencent.trtc.TRTCCloudDef.TRTC_APP_SCENE_VIDEOCALL;

/**
 * 视频通话事件监听类
 */
public class TRTCCloudListenerImpl extends TRTCCloudListener {

    private TXCloudVideoView mRemoteView, mLocalView;

    private TRTCCloud mTRrtcCloud; // TRTC SDK 实例对象

    private ITRTCNotify mNotify; // 房间状态改变通知

    private View mLoadView;

    public TRTCCloudListenerImpl(TXCloudVideoView remoteView, TXCloudVideoView localView, ITRTCNotify notify) {
        this.mNotify = notify;
        // 创建TRTCCloud单例
        mTRrtcCloud = TRTCCloud.sharedInstance(IM.staticContext);
        // TRTC SDK 视频通话房间进入所必须的参数
        TRTCCloudDef.TRTCVideoEncParam param = new TRTCCloudDef.TRTCVideoEncParam();
        param.videoResolution = TRTCCloudDef.TRTC_VIDEO_RESOLUTION_640_360;
        param.videoBitrate = 900;
        param.videoFps = 15;
        param.videoResolutionMode = TRTCCloudDef.TRTC_VIDEO_RESOLUTION_MODE_PORTRAIT;
        mTRrtcCloud.setVideoEncoderParam(param);
        mTRrtcCloud.setListener(this);
        this.mRemoteView = remoteView;
        this.mLocalView = localView;
    }

    @Override
    public void onError(int errCode, String errMsg, Bundle bundle) {
        super.onError(errCode, errMsg, bundle);
        mNotify.onError(errMsg);
    }

    /**
     * 加入房间
     * @param l
     */
    @Override
    public void onEnterRoom(long l) {
        super.onEnterRoom(l);
        if(mTRrtcCloud != null) {
            mTRrtcCloud.setLocalViewFillMode(TRTCCloudDef.TRTC_VIDEO_RENDER_MODE_FILL);
            mTRrtcCloud.startLocalPreview(true, mLocalView);
            //【重要】设置自定义采集数据方法【一定要开启】
            mTRrtcCloud.enableCustomVideoCapture(true);
            mTRrtcCloud.startLocalAudio();
            mNotify.onEnterRoom(l);
        }
    }

    /**
     * 离开房间
     */
    @Override
    public void onExitRoom(int i) {
        super.onExitRoom(i);
        if(mTRrtcCloud != null) {
            mTRrtcCloud.stopLocalPreview();
            mTRrtcCloud.stopAllRemoteView();
            exitRoom();
            mNotify.onExitRoom();
        }
    }

    /**
     * 有新的用户加入了当前视频房间
     */
    @Override
    public void onUserEnter(String userId) {
        super.onUserEnter(userId);
        if(mTRrtcCloud != null) {
            mTRrtcCloud.startRemoteView(userId, mRemoteView);
            mTRrtcCloud.setRemoteViewFillMode(userId, TRTCCloudDef.TRTC_VIDEO_RENDER_MODE_FILL);
            mNotify.onUserEnterRoom(userId);
        }
    }

    /**
     * 有用户离开了当前视频房间
     */
    @Override
    public void onUserExit(String userId, int reason) {
        super.onUserExit(userId, reason);
        if(mTRrtcCloud != null) {
            //停止观看画面
            mTRrtcCloud.stopRemoteView(userId);
            mTRrtcCloud.stopRemoteSubStreamView(userId);
            mTRrtcCloud.stopLocalPreview();
            exitRoom();
            mNotify.onUserExitRoom(userId);
        }
    }

    /**
     * 进入房间
     */
    public void enterRoom(String userId,String userSign,int appId,int roomId) {
        TRTCCloudDef.TRTCParams params = new TRTCCloudDef.TRTCParams();
        params.sdkAppId = appId;
        params.roomId = roomId;
        params.userId = userId;
        params.userSig = userSign;
        mTRrtcCloud.enterRoom(params, TRTC_APP_SCENE_VIDEOCALL);
    }

    /**
     * 退出房间
     */
    public void exitRoom() {
        if(mTRrtcCloud != null) {
            mTRrtcCloud.exitRoom();
            mTRrtcCloud.setListener(null);
            mTRrtcCloud = null;
            TRTCCloud.destroySharedInstance();
        }
    }


}
